//
//  CJCustomsheet.m
//  sheetCJ
//
//  Created by wy on 17/1/20.
//  Copyright © 2017年 wy. All rights reserved.
//

#import "CJCustomsheet.h"

#define KscreenWidth [UIScreen mainScreen].bounds.size.width
#define KscreenHeight [UIScreen mainScreen].bounds.size.height
@interface CJCustomsheet ()
{
    CGFloat contentViewHeight;
}
@property (nonatomic, strong)UIView *contentView;
@end

@implementation CJCustomsheet

static NSArray *allbus = nil;
-(CJCustomsheet *)initwithButtons:(NSArray *)allButtons{
    allbus = allButtons;
    CJCustomsheet *sheet = [[CJCustomsheet alloc] initWithFrame:CGRectMake(0, 0, KscreenWidth, KscreenHeight)];
    [sheet setView];
    return sheet;
}
-(void)setView{
    [UIView animateWithDuration:0.5 animations:^{
        _contentView.frame = CGRectMake(0, KscreenHeight - contentViewHeight, KscreenWidth, contentViewHeight);
    }];
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KscreenWidth, KscreenHeight)];
        backView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.3];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction)];
        [backView addGestureRecognizer:tap];
        [self addSubview:backView];
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, KscreenHeight, KscreenWidth, 0)];
        _contentView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_contentView];
        CGFloat supScHe = 0;
        if (allbus.count <6) {
            supScHe = 44 *allbus.count;
        }else{
            supScHe = 44 *6 + 22;
        }
        UIScrollView *superScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, supScHe)];
        
       
        superScrollerView.delegate = self;
        superScrollerView.showsHorizontalScrollIndicator = YES;
      
        superScrollerView.bounces = NO;
        
        superScrollerView.contentSize = CGSizeMake(KscreenWidth, allbus.count *44);
        
        [_contentView addSubview:superScrollerView];
       
        
       
        
        for (int i = 0; i< allbus.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = i;
            btn.backgroundColor = [UIColor whiteColor];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.frame = CGRectMake(0, 44 * i, KscreenWidth, 44);
            [superScrollerView addSubview:btn];
            [btn setTitle:allbus[i] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 43, KscreenWidth, 1)];
            line.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
            [btn addSubview:line];
        }
        UIView *intervalView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(superScrollerView.frame), KscreenWidth, 10)];
        intervalView.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
        
        UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(intervalView.frame), KscreenWidth, 44)];
        cancleBtn.backgroundColor = [UIColor whiteColor];
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancleBtn addTarget:self action:@selector(tapGestureAction) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        [_contentView addSubview:intervalView];
        [_contentView addSubview:cancleBtn];
        _contentView.height = supScHe + 44 + 10;
        contentViewHeight = _contentView.height;
        NSLog(@"%f",contentViewHeight);
        
        
    }
    return self;
}
-(void)clickBtn:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickButton:)]) {
        [self.delegate clickButton:btn.tag];
    }
    if (self.tapBtnClickBlock) {
        self.tapBtnClickBlock(btn.tag);
    }
    [self cancleAnimation];

}

-(void)tapGestureAction{
    [self cancleAnimation];

}

-(void)cancleAnimation{

    [UIView animateWithDuration:0.2 animations:^{
        _contentView.frame = CGRectMake(0, KscreenHeight, KscreenWidth, contentViewHeight);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
